package com.edol.painting.main;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.PopupMenu;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.doodle.common.utils.Utils;
import com.edol.painting.R;
import com.edol.painting.data.PaintingDatabase;
import com.doodle.common.utils.Future;
import com.doodle.common.utils.FutureListener;
import com.edol.painting.widget.EnhancePopupMenu;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/7/4.
 */

public class PaintPageFragment extends Fragment implements ViewPager.OnPageChangeListener,
        PaintingDatabase.PaintingChangedListener,
        View.OnClickListener {
    private ImageView mBackBtn;
    private ImageView mMoreBtn;
    private ImageView mShareBtn;

    private TextView mPagerIndexView;
    private ViewPager mPaintPagerView;

    private List<ImageView> mImgViewPool;
    private List<PaintingDatabase.PaintingEntry> mData;
    private int mIndex;
    private int mTotalPage;
    private EnhancePopupMenu mPopupMenu;

    private PaintPageAdapter mPaintPageAdapter;
    private PaintingDatabase.PaintingEntry mCurrentPaint;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_paint_page, null);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        PaintController.getInstances().hideActionBar(getActivity());
        PaintController.getInstances().lockDrawer(getActivity());
        PaintingDatabase.getInstance().addPaintingChangeListener(this);
        mImgViewPool = new ArrayList<ImageView>();

        initView();
        loadData();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        PaintingDatabase.getInstance().removePaintingChangeListener(this);
        PaintController.getInstances().showActionBar(getActivity());
        PaintController.getInstances().unlockDrawer(getActivity());
    }

    private void initView() {
        View root = getView();
        mBackBtn = (ImageView) root.findViewById(R.id.back);
        mMoreBtn = (ImageView) root.findViewById(R.id.more_btn);
        mShareBtn = (ImageView) root.findViewById(R.id.share_btn);

        mBackBtn.setOnClickListener(this);
        mMoreBtn.setOnClickListener(this);
        mShareBtn.setOnClickListener(this);

        mPagerIndexView = (TextView) root.findViewById(R.id.pager_index);
        mPaintPagerView = (ViewPager) root.findViewById(R.id.paint_pager);
    }

    private void loadData() {
        if (mPaintPageAdapter == null) {
            mPaintPageAdapter = new PaintPageAdapter(getActivity(), mData);
        }

        mPaintPagerView.setOnPageChangeListener(this);
        mPaintPagerView.setAdapter(mPaintPageAdapter);
        mPaintPagerView.setOffscreenPageLimit(2);
        if (mIndex > 0 && mIndex < mData.size()) {
            mPaintPagerView.setCurrentItem(mIndex);
        }
        mPagerIndexView.setText((mIndex + 1) + "/" + mTotalPage);
    }

    public void setData(List<PaintingDatabase.PaintingEntry> data, int index) {
        mData = data;
        mIndex = index;
        mTotalPage = data != null ? data.size() : 0;
        mCurrentPaint = mData != null && !data.isEmpty() ? mData.get(index) : null;
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        mIndex = position;

        mCurrentPaint = mData != null ? mData.get(position) : null;
        mPagerIndexView.setText((position + 1) + "/" + mTotalPage);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onPaintingListChanged() {
        if (mPaintPageAdapter != null) {
            mPaintPageAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                getActivity().onBackPressed();
                break;
            case R.id.more_btn:
                if (mCurrentPaint == null) {
                    return;
                }
                showMore();
                break;
            case R.id.share_btn:
                if (mCurrentPaint == null) {
                    return;
                }
                PaintingDatabase.getInstance().sharePainting(getActivity(), mCurrentPaint);
                break;
        }
    }

    private void showMore() {
        if (mPopupMenu == null) {
            mPopupMenu = new EnhancePopupMenu(getActivity(), mMoreBtn);
            mPopupMenu.inflate(R.menu.painting_pager);
            mPopupMenu.setGravity(Gravity.RIGHT);
            mPopupMenu.setOnMenuItemClickListener(new EnhancePopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    switch (item.getItemId()) {
                        case R.id.action_delete:
                            long[] ids = new long[1];
                            ids[0] = mCurrentPaint.mId;
                            PaintingDatabase.getInstance().deleteByIds(ids, true, new FutureListener() {
                                @Override
                                public void onFutureDone(Future future) {
                                    if (mPaintPageAdapter != null && mData.size() > 0) {
                                        getActivity().runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                mPaintPageAdapter.removePaint(mCurrentPaint);
                                                mTotalPage = mPaintPageAdapter.getCount();
                                                //mIndex = Utils.clamp(mIndex, 0, mData.size());
                                                //mPaintPagerView.setCurrentItem(mIndex);
                                            }
                                        });
                                    } else {
                                        getActivity().getSupportFragmentManager().popBackStack();
                                    }
                                }
                            });
                            break;
                        case R.id.action_export:
                            PaintingDatabase.getInstance().exportPainting(getActivity(), mCurrentPaint);
                            break;
                    }
                    return false;
                }
            });
        }

        int x = Utils.dipToPixel(40);
        int y = Utils.dipToPixel(10);
        mPopupMenu.show(x, y);

    }

}
